package com.itheima.controller;

import com.itheima.sevice.CheckGroupService;
import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {

    @Autowired
    private CheckGroupService checkGroupService;

    /**
     * 新增检查组
     * @param checkitemIds
     * @param checkGroup
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestParam List<Integer> checkitemIds, @RequestBody CheckGroup checkGroup){
        Integer recordCount = checkGroupService.add(checkitemIds, checkGroup);

        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS,recordCount);
    }

    /**
     * 根据id删除检查组
     * @param id
     * @return
     */
    @GetMapping("/deleteById")
    public Result deleteById(Integer id){
        Integer recordCount = checkGroupService.deleteById(id);

        return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS, recordCount);
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = checkGroupService.findPage(queryPageBean);

        return pageResult;
    }

    /**
     * 根据套餐id查询关联的检查项id
     * @param setmealId
     * @return
     */
    @GetMapping("/findCheckGroupIdsBySetmealId")
    public Result findCheckGroupIdsBySetmealId(Integer setmealId){
        List<Integer> setmealIds = checkGroupService.findCheckGroupIdsBySetmealId(setmealId);

        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, setmealIds);
    }

    /**
     * 编辑检查组
     * @param checkitemIds
     * @return
     */
    @PostMapping("/edit")
    public Result edit(@RequestParam List<Integer> checkitemIds, @RequestBody CheckGroup checkGroup){
        Integer recordCount = checkGroupService.update(checkitemIds, checkGroup);

        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS, recordCount);
    }

    /**
     * 查询全部检查组
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll(){
        List<CheckGroup> checkGroupList = checkGroupService.findAll();

        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroupList);
    }

    /**
     * 根据id查询检查组
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(Integer id){
        CheckGroup checkGroup = checkGroupService.findById(id);

        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroup);
    }
}
